package com.ruitu.mall_98_2.mine.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.beanu.arad.base.BaseFragment;
import com.beanu.arad.utils.ToastUtils;
import com.ruitu.mall_98_2.R;
import com.ruitu.mall_98_2.dao.APIRetrofit;
import com.ruitu.mall_98_2.mine.CheckBillActivity;
import com.ruitu.mall_98_2.mine.WithdrawDepositActivity;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.bean.api.BaseResult;
import com.ruitu.router_module.bean.api.SubscriberImpl;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

// 钱包--余额fragment
public class BalanceFragment extends BaseFragment {

    private TextView tv_balance;
    private TextView tv_withdraw_deposit;
    private TextView tv_check_order;

    public static BalanceFragment newInstance() {
        Bundle args = new Bundle();
        BalanceFragment fragment = new BalanceFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                getBalance();
            }
        }, 300);
    }

    // 获取余额信息
    private void getBalance() {
        APIRetrofit.getDefault().getBalance(AppHolder.instance().getUser().getAccountId())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new SubscriberImpl<BaseResult<String>>() {
                    @Override
                    public void onError(Throwable e) {
                        ToastUtils.showShort("获取余额失败");
                        tv_balance.setText("获取余额失败");
                    }

                    @Override
                    public void onNext(BaseResult<String> stringBaseResult) {
                        if (stringBaseResult.isSuccess()) {
//                            setData(stringBaseResult.getData() + "");
                            tv_balance.setText("￥" + stringBaseResult.getData() + "");
                        } else {
                            ToastUtils.showShort(stringBaseResult.getMsg());
                            tv_balance.setText("获取余额失败");
                        }
                    }
                });
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_balance, container, false);

        tv_balance = v.findViewById(R.id.tv_balance);
        tv_withdraw_deposit = v.findViewById(R.id.tv_withdraw_deposit);
        tv_check_order = v.findViewById(R.id.tv_check_order);
        tv_withdraw_deposit.setOnClickListener(this);
        tv_check_order.setOnClickListener(this);

        return v;
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (tv_withdraw_deposit == v) {
            startActivity(WithdrawDepositActivity.class);
        }

        if (tv_check_order == v) {
            startActivity(CheckBillActivity.class);
        }
    }
}
